<template>
	<view class="index">
		<view class="banner">
			<view class="title">
				<text class="name">源聚变</text>
				<view class="search">
					<input @focus="focus" class="search-input" placeholder-style="color:#fff" type="text" value="" placeholder="百事可乐" />
					<image class="search-img" src="../../static/index/search.png" mode=""></image>
				</view>
			</view>
			<u-swiper height="360" :list="list1" @click="click"></u-swiper>
		</view>
		<!-- 五个模块 -->
		<view class="modules">
			<view class="modules-box" v-for="(item,index) in modulesList" :key="index" @click="clickModules(item)">
				<image :src="item.img" mode=""></image>
				<text class="modules-name">{{item.name}}</text>
			</view>
		</view>
		<view class="banner2">
			<image src="../../static/index/banner2.png" mode=""></image>
		</view>
		<view class="select-box">
			<!-- <view :class="{active:num==0}"  @click="num=0" style="font-size: 32rpx;"> -->
			<view :class="{active:num==0}"  @click="tabGoods(0)" style="font-size: 32rpx;">
				综合
			</view>  
			<view :class="{active:num==1}"  @click="tabGoods(1)" style="font-size: 32rpx;">
				兑换量
			</view>
			<view style="display: flex;align-items: center;" @click="num=2">
				<text :class="{active:num==2}" style="font-size: 32rpx;">积分</text>
				<view class="botton-btn">
					<image style="width: 14rpx;height: 12rpx;" @click="integralbtn(0)" v-if="topShow" src="../../static/earn/shang.png" mode=""></image>
					<image style="width: 14rpx;height: 12rpx;" @click="integralbtn(0)" v-else src="../../static/earn/shangactive.png" mode=""></image>
					<image style="width: 14rpx;height: 12rpx;margin-top: 8rpx;" @click="integralbtn(1)" v-if="bottomShow" src="../../static/earn/xia.png" mode=""></image>
					<image style="width: 14rpx;height: 12rpx;margin-top: 8rpx;" @click="integralbtn(1)" v-else src="../../static/earn/xiaactive.png" mode=""></image>
				</view>
			</view>
			
			
			<view :class="{active:num==3}" @click="jumpScreen" style="font-size: 32rpx;">
				筛选<image class="screen-img" src="../../static/index/screen.png" mode=""></image>
			</view>
		</view>
		<!-- 热词 -->
		<view class="hot">
			<view class="hot-box" v-for="(item,index) in hotList" :key="index">
				<image style="width: 16rpx;height: 18rpx;margin-right: 15rpx;" v-if="index==0" src="../../static/index/hot.png" mode=""></image><text :class="{hotActive:index==0}">{{item.name}}</text>
			</view>
		</view>
		<view class="shops" v-if="num==0">
			<view class="shop-box" v-for="(item,index) in shopsList" :key='index' @click="clickDetail(item)">
				<view class="imgbox">
					<image :src="item.image" mode=""></image>
				</view>
				<text class="describe">{{item.name}}</text>
				<view class="integral">
					<text>{{item.price}}积分</text>
					<text>已兑换<text class="num">{{item.sales}}</text>件</text>
				</view>
				<view class="price">原价 <text>￥{{item.original_price}}</text></view>
				<view class="shops-bottom">
					<view class="shop-car" @click.stop="addShopCar(item)">
						<image src="../../static/healthy/shopcar.png" mode=""></image>
					</view>
					<view class="conversion">
						立即兑换
					</view>
				</view>
			</view>

		</view>
		<view v-if="num==1" class="shops">
			<view class="shop-box" v-for="(item,index) in shopsList" :key='index'>
				<view class="imgbox">
					<image :src="item.image" mode=""></image>
				</view>
				<text class="describe">{{item.name}}</text>
				<view class="integral">
					<text>{{item.price}}积分</text>
					<text>已兑换<text class="num">{{item.sales}}</text>件</text>
				</view>
				<view class="price">原价 <text>￥{{item.original_price}}</text></view>
				<view class="shops-bottom">
					<view class="shop-car">
						<image src="../../static/healthy/shopcar.png" mode="" @click.stop="addShopCar(item)"></image>
					</view>
					<view class="conversion">
						立即兑换
					</view>
				</view>
			</view>
		</view>
		<view v-if="num==2" class="shops">
			<view class="shop-box" v-for="(item,index) in shopsList" :key='index'>
				<view class="imgbox">
					<image :src="item.image" mode=""></image>
				</view>
				<text class="describe">{{item.name}}</text>
				<view class="integral">
					<text>{{item.price}}积分</text>
					<text>已兑换<text class="num">{{item.sales}}</text>件</text>
				</view>
				<view class="price">原价 <text>￥{{item.original_price}}</text></view>
				<view class="shops-bottom">
					<view class="shop-car">
						<image src="../../static/healthy/shopcar.png" mode="" @click.stop="addShopCar(item)"></image>
					</view>
					<view class="conversion">
						立即兑换
					</view>
				</view>
			</view>
		</view>
		<u-popup :show="show" v-model="show" mode="bottom" :round="true">
		            <view style="width: 100%;height: auto;background: #FFFFFF;padding: 40rpx;">
		                <size-pop @send="getSonValue" :sizeArr="sizeArr" :obj="obj"></size-pop>
		                <!-- <size-pop :test="isTest" @send="getSonValue" :sizeArr="sizeArr" :obj="obj"></size-pop> -->
		            </view>
		</u-popup>
		<tabbar></tabbar>
	</view>
</template>

<script>
	import {getLogin} from '@/service/user.js';//登录
	import {getBanner} from '@/service/banner.js';//广告
	import {getGoodsList,hotSearch,getGoodsDetails} from '@/service/goods.js';//商品列表 热门搜索 商品详情
	import sizePop from '../earn/sizePop'
	import {
		tabbar
	} from "../../components/tabbar/tabbar.vue" //底部导航栏
	export default {
		components: {
			tabbar,
			sizePop
		},
		data() {
			return {
				obj:{},//商品信息
				show:false,//关闭弹窗
				hotList:[
					// {text:'热门'},{text:'大家都在说'},{text:'性价比之王'},
					],
				num:0,
				list1: [],//轮播列表
				modulesList: [{
					img: require('../../static/index/agrotechny.png'),
					name: '农特优选',
					url:'/myPackageA/index/regionSelection/regionSelection',
					// cls:1,  
				}, {
					img: require('../../static/index/help.png'),
					name: '助力实体',
					url:'/myPackageA/index/powerEntity',
					cls:2,
				}, {
					img: require('../../static/index/customize.png'),
					name: '私定精选',
					url:'/myPackageA/index/togetherSelected',
					cls:3,
				}, {
					img: require('../../static/index/public.png'),
					name: '公益环保',
					url:'/myPackageA/index/publicWelfare'  
				}, {
					img: require('../../static/index/medical.png'),
					name: '医疗健康',
					url:'/myPackageA/index/healthcare',
					cls:4,
				}, ],
				shopsList:[],
				userIp:'',//用户ip
				totalPage:0,//总页数
				params:{
					sort:0,//3--升序 4--降序
					page:1,
					limit:20,
					two_class:null,//二级id
				},
				topShow:true,//积分上箭头
				bottomShow:true,//积分下箭头
				sizeArr:[],//购物车规格数组
				id:'',//商品id
				goods_id:'',//商品详情id
			}
		},
		onShow() {
			this.num = 0;
			this.topShow=true;
			this.bottomShow=true;
			this.getDoodsAll();
			
		},
		onLoad() {
				this.getBanner();
				this.getUserIp();
				this.getHot()
				
		},
		onReachBottom() {//滚动到底翻页
			this.more()
		 },
		methods: {
			//关闭弹框
			getSonValue(res){
				this.show = res;//关闭弹框
			},
			//获取热词
			getHot(){
				hotSearch().then(res=>{
					this.hotList = res.data
				})
			},
			//跳转到商品详情
			clickDetail(item){
				this.id = item.id
				let param = {
					id:item.id
				}
				let url = '/myPackageA/earn/earnDeail'
				this.$navTo.togo(url,param)
			},
			//获取商品规格
			getGoodsDetails(){
				let params = {
					goods_id:this.goods_id
				}
				getGoodsDetails(params).then(res=>{
					let arr = res.data.spec_relation
					this.obj = {
						name:res.data.name,
						price:res.data.price,
						original_price:res.data.original_price,
						image:res.data.image
					}
					let new_arr = arr.map((item,index)=>{
						return Object.assign(item,{showFlag:0})
					})
					this.sizeArr = new_arr
				})
			},
			//加入购物车
			addShopCar(item){
				this.goods_id=item.id
				this.getGoodsDetails();
				this.show = true;
			},
			//商品切换模块
			tabGoods(type){
				this.topShow = true
				this.bottomShow = true
				if(type==0){//综合
					this.num=0
					this.params.sort=0
					this.getDoodsAll();
				}else if(type==1){//兑换量
					this.num = 1
					this.params.sort=2
					this.getDoodsAll();
				}
			},
			// 五个模块跳转
			clickModules(item){
				let url = item.url
				let param = {
					cls:item.cls
				}
				this.$navTo.togo(url,param)
			},
			//筛选
			jumpScreen(){
				uni.navigateTo({
					url:'/myPackageA/index/screen/screen'
				})
			},
			//积分箭头
			integralbtn(type){
				if(type==0){//升序
					this.topShow = false
					this.bottomShow = true
					this.params.sort = 3
					this.getDoodsAll()
				}else if(type==1){//降序
					this.bottomShow = false
					this.topShow = true
					this.params.sort = 4
					this.getDoodsAll()
				}
			},
			//获取焦点跳转到搜索页面
			focus(){
				uni.navigateTo({
					url:'/myPackageA/search/search'
				})
			},
			//获取用户ip
			getUserIp(){
				let that = this;
				uni.request({
				    url: 'http://ip-api.com/json',
				    success: function (res) {
				        // console.log("ip =>", res.data.query, res.data);
						that.userIp = res.data.query;
						//登录
						let params = {
							phone:'18190516712',
							password:'111111',
							cls:'1',
							ip:that.userIp
						}
						getLogin(params).then(res=>{
							uni.setStorageSync('token',res.data.token); 
						})
				    }
				});
			},
			//获取首页banner
			getBanner(){
				let params = {
					type:'1'
				}
				getBanner(params).then(res=>{
					// console.log(res,'获取banner');
					this.list1 = res.data
				})
			},
			//商品列表
			getDoodsAll(isMore){
				//获取所有的商品列表
				getGoodsList(this.params).then(res=>{
					if(res.code=='200'){
						const list = res.data.data
						const total = res.data.total
						if(list.length===0){
							uni.showToast({ title: '暂无内容', icon: 'none' })
							return false
						}
						this.totalPage = Math.ceil(Number(total) / 18)
						this.shopsList = isMore ? this.shopsList.concat(list) : list
					}else{
						 uni.showToast({ title: res.msg || '获取列表失败', icon: 'none' })
					}
				})
			},
			//加载获取更多
			more() {
				if (this.params.page >= this.totalPage) {
					uni.showToast({ title: '没有更多啦~', icon: 'none' })
					return false
				}
				this.params.page++
				this.getDoodsAll(true)
			},
		}
	}
</script>

<style scoped lang="less">
	.botton-btn{
		display: flex;
		flex-direction: column;
		margin-left: 10rpx;
	}
	.hotActive{
		color: #E7402E;
	}
	.hot{
		padding: 40rpx;
		display: flex;
		flex-wrap: wrap;
		.hot-box{
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 10rpx 20rpx;
			height: 36rpx;
			background: #FFFFFF;
			opacity: 1;
			border-radius: 18rpx;
			font-size: 20rpx;
			font-family: PingFang SC;
			color: #666666;
			margin-right: 20rpx;
		}
	}
	.screen-img{
		width: 21rpx;
		height: 22rpx;
		margin-left: 15rpx;
	}
	.active{
		font-size: 32rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #E7402E;
	}
	.tabs-box{
		font-size: 32rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #666666;
	}
	.select-box{
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
	.shops {
		width: 100%;
		padding: 40rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
		.shop-box {
			margin-right: 20rpx;
			width: 30%;
			height: auto;
			margin-bottom: 34rpx;
			background: #FFFFFF;
			box-shadow: 0px 4rpx 8rpx rgba(0, 0, 0, 0.16);
			border-radius: 10rpx;
			display: flex;
			flex-direction: column;
			position: relative;
			.imgbox {
				// width: 212rpx;
				width: 100%;
				height: 230rpx;
				padding: 6rpx;
				border-radius: 10rpx;
				image {
					border-radius: 10rpx;
					width: 100%;
					height: 100%;
				}
			}

			.describe {
				font-size: 24rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				line-height: 24rpx;
				color: #333333;
				margin-top: 20rpx;
			}

			.integral {
				padding: 20rpx 0;
				line-height: 0;
				text:first-child {
					font-size: 20rpx;
					font-family: Source Han Sans CN;
					font-weight: bold;
					color: #000000;
				}

				text:last-child {
					font-size: 12rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #999999;
					// margin-left: 14rpx;

					.num {
						color: #000000;
						font-size: 14rpx;
					}
				}
			}

			.price {
				font-size: 16rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #FF7B7B;
				padding-bottom: 57rpx;
				text {
					text-decoration: line-through
				}
			}

			.shops-bottom {
				position: absolute;
				bottom: 0;
				left: 0;
				height: 44rpx;
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				.shop-car {
					width: 100rpx;
					height: 100%;
					background: #FF7200;
					opacity: 0.8;
					border-radius: 0px 0px 0px 10rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					image{
						width: 22rpx;
						height: 20rpx;
					}
				}

				.conversion {
					flex: 1;
					height: 100%;
					background: #FF0000;
					opacity: 0.8;
					border-radius: 0px 0px 10rpx 0rpx;
					color: #FFFFFF;
					font-size: 24rpx;
					text-align: center;
					line-height: 44rpx;
				}
			}
		}
	}

	.modules {
		display: flex;
		// justify-content: space-between;
		width: 100%;
		height: auto;
		padding: 60rpx 46rpx 20rpx;

		.modules-box {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;

			image {
				width: 92rpx;
				height: 92rpx;
			}

			.modules-name {
				font-size: 28rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #000000;
				margin-top: 20rpx;
			}
		}
	}

	/deep/.u-swiper-indicator {
		margin: 0 -240rpx;
	}

	.title {
		display: flex;
		align-items: center;
		width: 100%;
		height: 100rpx;
		z-index: 9;
		position: absolute;
		top: 30rpx;
		left: 0;

		.name {
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			padding: 0 40rpx;
		}

		.search {
			position: relative;

			.search-input {
				// background-color: rgba(225,225,225,0.22);
				background-color: #857561;
				border-radius: 30rpx;
				padding-left: 60rpx;
				width: 302rpx;
				height: 64rpx;
				z-index: 99;
			}

			.search-img {
				width: 32rpx;
				height: 32rpx;
				position: absolute;
				top: 16rpx;
				left: 18rpx;
			}
		}
	}

	.banner {
		position: relative;
	}

	.banner2 {
		width: 100%;
		height: 248rpx;
		margin-bottom: 20rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>
